﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using BSCMFF.Core.DataAccessLayer;
using BSCMFF.Core.BusinessLayer;
using System.Collections;
using System.Data;
using BSCMFF.Core;

namespace BSCMFF.Web.Pages
{
    public partial class UserManagement : MultiLanguagePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void RadGrid1_InsertCommand(object source, GridCommandEventArgs e)
        {
            GridEditableItem editedItem = e.Item as GridEditableItem;
            UserControl userControl = (UserControl)e.Item.FindControl(GridEditFormItem.EditFormUserControlID);

            //BSCEntities context = new BSCEntities();

            User u = new User();

            SetUserProperties(userControl, u);
            //u.Role = new Role();

            //u.Login = "agtwe";
            //u.Password = "adsg";
            //u.Role_FK = 1;
            //u.User_Setting_FK = 1;
            UserManager um = new UserManager();
            um.CreateUser(u);

            //context.Users.AddObject(u);


            //context.SaveChanges();

        }

        private static void SetUserProperties(UserControl userControl, User u)
        {
            u.Firstname = (userControl.FindControl("RadTextBoxFirstname") as RadTextBox).Text;
            u.Lastname = (userControl.FindControl("RadTextBoxLastname") as RadTextBox).Text;
            u.Email = (userControl.FindControl("RadTextBoxEmail") as RadTextBox).Text;
            u.Login = (userControl.FindControl("RadTextBoxLogin") as RadTextBox).Text;
            u.Password = (userControl.FindControl("TextBoxPassword") as RadTextBox).Text;
            u.Role_FK = Convert.ToInt32((userControl.FindControl("ComboBoxRoles") as RadComboBox).SelectedValue);
            u.User_Setting_FK = 1;
        }
        protected void RadGrid1_DeleteCommand(object source, GridCommandEventArgs e)
        {
            GridEditableItem editedItem = e.Item as GridEditableItem;
            UserControl userControl = (UserControl)e.Item.FindControl(GridEditFormItem.EditFormUserControlID);


            int id = Convert.ToInt32(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ID"].ToString());

            UserManager um = new UserManager();
            um.Delete(id);

            //BSCEntities context = new BSCEntities();

            //User u = (from user in context.Users
            //          where user.ID == id
            //          select user).Single();

            //context.Users.DeleteObject(u);
            //context.SaveChanges();
            RadGrid1.Rebind();
        }

        protected void RadGrid1_UpdateCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            GridEditableItem editedItem = e.Item as GridEditableItem;
            UserControl userControl = (UserControl)e.Item.FindControl(GridEditFormItem.EditFormUserControlID);


            int user_ID = Convert.ToInt32(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ID"].ToString());
            BSCEntities context = new BSCEntities();

            //User u = (from user in context.Users
            //          where user.ID == id
            //          select user).Single();

            //Hashtable ht = new Hashtable();
            //ht["login"] = (userControl.FindControl("TextBoxLogin") as RadTextBox).Text;
            ////u.Login = (userControl.FindControl("TextBoxLogin") as RadTextBox).Text;
            //ht["password"] = (userControl.FindControl("TextBoxPassword") as RadTextBox).Text;            
            ////u.Password = (userControl.FindControl("TextBoxPassword") as RadTextBox).Text;
            //ht["role_fk"] = Convert.ToInt32((userControl.FindControl("ComboBoxRoles") as RadComboBox).SelectedValue);
            ////u.Role_FK = Convert.ToInt32((userControl.FindControl("ComboBoxRoles") as RadComboBox).SelectedValue);
            ////context.SaveChanges();

            User u = new User();
            u.ID = user_ID;
            SetUserProperties(userControl, u);

            UserManager um = new UserManager();
            um.Update(u);
        }

        //protected void RadGrid1_SelectedIndexChanged(object source, GridCommandEventArgs e)
        //{
        //    Response.Redirect("UserList.aspx?EmployeeID=" + RadGrid1.SelectedValue);
        //}

        //protected void RadGrid1_SelectedIndexChanged1(object sender, EventArgs e)
        //{
            
        //}

        protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.Item.ItemIndex != -1 && e.CommandName == "Select") //create new
            {
                int id = Convert.ToInt32(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ID"].ToString());
                Response.Redirect("EmployeeInfo.aspx?EmployeeID=" + id);
            }
        }

        //protected void ImageButtonImportUsers_Click(object sender, ImageClickEventArgs e)
        //{
            
        //}

        
    }
}